package com.programmercarl.string;

/**
 * https://leetcode.cn/problems/repeated-substring-pattern/
 *
 * @author yulongtian
 * @create 2024-12-15 15:42
 */
public class Test07 {
    public static void main(String[] args) {
        Test07 test07 = new Test07();
        System.out.println(test07.repeatedSubstringPattern("abaababaab"));
    }

    public boolean repeatedSubstringPattern(String s) {
        int length = s.length();
        if (length == 1) {
            return false;
        }
        for (int i = 0; i < length - 1; i++) {
            if (i == 0 || length % (i + 1) == 0) {
                if (check(s, s.substring(0, i + 1))) {
                    return true;
                }
            }
        }
        return false;
    }

    public boolean check(String s, String t) {
        for (int i = 0; i < s.length(); i += t.length()) {
            if (!s.startsWith(t, i)) {
                return false;
            }
        }
        return true;
    }

}
